package com.redijedi.tapestry.pages;

import java.util.ArrayList;
import java.util.List;

import org.apache.tapestry.annotations.Component;
import org.apache.tapestry.annotations.SetupRender;

import com.redijedi.tapestry.ITreeNodeModel;
import com.redijedi.tapestry.components.Tree;
import com.redijedi.tapestry.internal.tree.AbstractTreeNodeModel;
import com.redijedi.tapestry.test.Widget;

/**
 * @author torr
 * 
 */
public class TreeTestPage {

	@SuppressWarnings("unused")
	@Component(parameters = { "model=model" })
	private Tree<Widget> _tree;

	@SuppressWarnings("unused")
	@Component(parameters = { "model=model" })
	private Tree<Widget> _treeWithContent;

	private Widget _loopValue;

	private Widget _widget;

	private ITreeNodeModel<Widget> _model;

	public Widget getLoopValue() {
		return _loopValue;
	}

	public void setLoopValue(Widget value) {
		_loopValue = value;
	}

	public ITreeNodeModel<Widget> getModel() {
		return _model;
	}

	@SetupRender
	void begin() {
		_widget = getWidget();
		_model = new TreeNode(_widget);
	}

	private Widget getWidget() {
		Widget w = new Widget("0.0 - ROOT");
		List<Widget> widgets = new ArrayList<Widget>();
		widgets.add(new Widget("1.0 - LEAF"));
		widgets.add(new Widget("1.1 - LEAF"));
		widgets.add(new Widget("1.2 - LEAF"));
		widgets.add(new Widget("1.3 - LEAF"));
		Widget w2 = new Widget("1.4 - BRANCH");
		Widget w3 = new Widget("2.0 - LEAF");
		w2.getChildren().add(w3);
		widgets.add(w2);
		w.setChildren(widgets);
		return w;
	}

	private class TreeNode extends AbstractTreeNodeModel<Widget> {
		TreeNode(Widget widget) {
			_parent = null;
			for (Widget w : widget.getChildren()) {
				TreeNode node = new TreeNode(w);
				node._parent = this;
				_children.add(node);
			}
			_value = widget;
		}
	}
}
